package com.huaao.micropolice.root.commons;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class RequestUtil {
	
	/**
	 * 获得用户远程地址
	 */
	public static String getRemoteAddr(HttpServletRequest request){
		String remoteAddr = request.getHeader("X-Real-IP");
        if (StringUtils.isNotBlank(remoteAddr)) {
        	remoteAddr = request.getHeader("X-Forwarded-For");
        }else if (StringUtils.isNotBlank(remoteAddr)) {
        	remoteAddr = request.getHeader("Proxy-Client-IP");
        }else if (StringUtils.isNotBlank(remoteAddr)) {
        	remoteAddr = request.getHeader("WL-Proxy-Client-IP");
        }
        return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
	}

	public static Object buildParam(HttpServletRequest request) {
		Map<String, String[]> paramMap =  request.getParameterMap();
		if(paramMap == null){
			return "";
		}
    	StringBuilder buff = new StringBuilder();
    	for(String key : paramMap.keySet()){
    		buff.append(key + "=");
    		String[] values = paramMap.get(key);
    		if(values == null || values.length == 0){
    			buff.append("&");
    		}else if(values.length == 1){
    			buff.append(values[0] + "&");
    		}else{
    			buff.append("[");
    			for(int i = 0 ; i < values.length ; i++){
    				if(i < values.length - 1){
    					buff.append(values[i] + ",");
    				}else{
    					buff.append(values[i]);
    				}
    			}
    			buff.append("]&");
    		}
    	}
    	return buff.toString();
	}
}
